﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Vit.Data
{
    public class SqlJoin : SqlClause
    {
        public SqlJoin()
        {
            Join = JoinType.InnerJoin;
            On = new List<SqlLogic>();
        }

        public JoinType Join { get; set; }
        public SqlObject Object { get; set; }
        public List<SqlLogic> On { get; set; }

        public override string ToSql()
        {
            StringBuilder sb = new StringBuilder();
            switch (Join)
            {
                default:
                case JoinType.InnerJoin:
                    sb.Append("INNER JOIN");
                    break;
                case JoinType.LeftJoin:
                    sb.Append("LEFT JOIN");
                    break;
                case JoinType.RightJoin:
                    sb.Append("RIGHT JOIN");
                    break;
                case JoinType.OuterJoin:
                    sb.Append("OUTER JOIN");
                    break;
            }
            sb.Append(" ");
            sb.Append(Object.ToSql());
            sb.Append(" ON ");
            foreach (var logic in On)
            {
                sb.Append(logic.ToSql());
            }
            return sb.ToString();
        }
    }

    public enum JoinType
    {
        LeftJoin, InnerJoin, OuterJoin, RightJoin
    }
}
